"""
模拟鼠标操作双击 右击 悬浮、根据坐标点拖动

"""

import time

from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.maximize_window()
driver.get("https://sahitest.com/demo/clicks.htm")
actions = ActionChains(driver)  # 实例化键盘操作
element1 = driver.find_element(By.CSS_SELECTOR, "body > form > input[type=button]:nth-child(8)")
# 双击
actions.double_click(element1).perform()  # 用键盘操作代替点击element
time.sleep(3)
# 右击
element2 = driver.find_element(By.XPATH, "/html/body/form/input[4]")
actions.context_click(element2).perform()
time.sleep(3)
# 悬浮
driver.get("https://www.baidu.com")
element3 = driver.find_element(By.XPATH, '//*[@id="s-top-left"]/div/a')
actions.move_to_element(element3).perform()  # 把鼠标移动到悬浮处
driver.find_element(By.CSS_SELECTOR, "a[href='https://baike.baidu.com']").click()
# 拖动
driver.get("https://sahitest.com/demo/dragDropMooTools.htm")
element_start=driver.find_element(By.ID,"dragger")
element_end=driver.find_element(By.XPATH,"/html/body/div[5]")
actions.drag_and_drop(element_start,element_end).perform()
time.sleep(3)
# 根据坐标点拖动
driver.get("https://iviewui.com/view-ui-plus/component/form/slider")
element4 = driver.find_element(By.XPATH, "//div[@class='ivu-slider-button']")
actions.drag_and_drop_by_offset(element4, 250, 0).perform()  # 移动像素横坐标,纵坐标
time.sleep(3)
driver.close()
